package com.jiyun.jiyunmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.jiyunmall.common.service.impl.CrudServiceImpl;
import com.jiyun.jiyunmall.product.dao.AttrDao;
import com.jiyun.jiyunmall.product.dto.AttrDTO;
import com.jiyun.jiyunmall.product.entity.AttrEntity;
import com.jiyun.jiyunmall.product.service.AttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品属性
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-07-12
 */
@Service
public class AttrServiceImpl extends CrudServiceImpl<AttrDao, AttrEntity, AttrDTO> implements AttrService {

    @Autowired
    AttrDao attrDao;
    @Override
    public QueryWrapper<AttrEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<AttrDTO> getByLikeName(String attrName) {
        QueryWrapper<AttrEntity> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(attrName),"attr_name",attrName);
        List<AttrEntity> attrEntities = attrDao.selectList(qw);
        List<AttrDTO> dtos = new ArrayList<AttrDTO>();
        for (AttrEntity attrEntity : attrEntities) {
            AttrDTO attrDTO = new AttrDTO();
            attrDTO.setAttrId(attrEntity.getAttrId());
            attrDTO.setAttrName(attrEntity.getAttrName());
            dtos.add(attrDTO);

        }
        return dtos;
    }
}